<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends WD_Controller {


	public function index(){
		$this->load->view('login/index');
	}

	public function asyncLogin(){

		$grant_type = isset($_POST["grant_type"]) ? addslashes($_POST["grant_type"]) : "";
		$username = isset($_POST["username"]) ? addslashes($_POST["username"]) : "";
		$password = isset($_POST["password"]) ? addslashes($_POST["password"]) : "";
		$client_id = isset($_POST["client_id"]) ? addslashes($_POST["client_id"]) : "";

		$data = [
			'grant_type' => $grant_type,
			'username' => $username,
			'password' => $password,
			'client_id' => $client_id
		];

		//print_r($data);

        $oauth_root = $this->config->item('oauth_root');
		$request_url = $oauth_root."/oauth2/token";
		//echo $request_url;exit;

		$r = curl_post_header($request_url, [], $data);

		if($r2 = json_decode($r, true)){
		    //print_r($r2);
			if(isset($r2["code"]) && $r2["code"]==200){
				if(isset($r2["body"]["access_token"]) && !empty($r2["body"]["access_token"])){
					$this->load->library('session');
					$access_token = $r2["body"]["access_token"];
					$_SESSION["access_token"] = $access_token;

                    $yuxun_root = $this->config->item('yuxun_root');
                    $url = $yuxun_root."/v1/user/my-info";

                    $header = ["Authorization:Bearer ".$access_token];

                    $r3 = \libraries\Curl::curl_get_request($url, [], $header);

                    if($myInfo = json_decode($r3, true)){

                        if(isset($myInfo["error_code"]) && $myInfo["error_code"]==0 && $body = $myInfo["body"]){
                            //print_r($body);

                            $this->uid = isset($body["uid"]) ? $body["uid"] : 0;
                            $this->username = isset($body["username"]) ? $body["username"] : "";
                            $this->nickname = isset($body["nickname"]) ? $body["nickname"] : "";

                            $_SESSION["uid"] = $this->uid;
                            $_SESSION["nickname"] = $this->nickname;
                            $_SESSION["nickname"] = $this->nickname;
                            $_SESSION["my_info"] = $body;

                            exit(json_encode(["code"=>1, "info"=>"成功"], JSON_UNESCAPED_UNICODE));
                        }else{
                            exit(json_encode(["code"=>-5, "info"=>"查看".$url."失败", "data"=>$r3]));
                        }
                    }else{
                        exit(json_encode(["code"=>-4, "info"=>"失败", "data"=>$r2]));
                    }
				}else{
                    exit(json_encode(["code"=>-3, "info"=>"失败", "data"=>$r2]));
                }
			}else{
				exit(json_encode(["code"=>-2, "info"=>"失败", "data"=>$r2]));
			}
		}else{
			exit(json_encode(["code"=>-1, "info"=>"失败", "data"=>$r]));
		}

	}

    /**
     * 登出
     */
	public function logout(){
        $this->load->library('session');
        $_SESSION["access_token"] = '';
        $_SESSION["uid"] = '';
        $_SESSION["nickname"] = '';
        $_SESSION["nickname"] = '';
        header("Location:/");
    }

}
